package com.ys.controller.api.finance;

import com.ys.config.MenuId;
import com.ys.entity.finance.TcwWanliSupplementary;
import com.ys.entity.finance.TcwWanliSupplementaryItem;
import com.ys.entity.sys.YsCol;
import com.ys.enume.sys.YsColEnum;
import com.ys.service.finance.TcwWanliSupplementaryService;
import com.ys.service.flow.YspTaskService;
import com.ys.service.log.ApiLogService;
import com.ys.service.sys.CommonApiService;
import com.ys.service.sys.UploadService;
import com.ys.util.ExportExcelUtils;
import com.ys.util.shiro.ShiroUtils;
import com.ys.vo.ApiResponse;
import com.ys.vo.LayuiTableVoJA;
import com.ys.vo.R;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * 万里汇账号资金
 */
@Controller
@RequestMapping("/api/inter/tcwWanliSupplementary")
public class ApiInterTcwWanliSupplementaryController {
    private static final Logger logger = LoggerFactory.getLogger(ApiInterTcwWanliSupplementaryController.class);

    @Autowired
    private CommonApiService<TcwWanliSupplementary> commonApiService;

    @Autowired
    private CommonApiService<TcwWanliSupplementaryItem> commonApiServiceItem;

    @Autowired
    private TcwWanliSupplementaryService tcwWanliSupplementaryService;


    @Autowired
    private YspTaskService yspTaskService;


    @RequiresPermissions("tcwWanliSupplementary:view")
    @RequestMapping("/search")
    @ResponseBody
    public LayuiTableVoJA search(@ModelAttribute TcwWanliSupplementaryItem cond) {

        cond = setSeachSql(cond);
        cond.setOrderby("A.Id DESC");
        return commonApiServiceItem.searchLay(cond);
    }

    /**
     * 设置查询的cond的sql设置
     *
     * @param cond
     * @return
     */
    public TcwWanliSupplementaryItem setSeachSql(TcwWanliSupplementaryItem cond) {
        cond.setOrderby("A.createTime DESC")
                .addLeftJoin("TCW_WanliSupplementary B ON B.Bid = A.Pbid")
                .addField("B.Bid AS bid")
                .addField("B.Remark AS remark")
                .addField("C.Code AS depositPlatformCode")
                .addField("B.EffectStock AS effectStock")
                .addLeftJoin("TS_UserChange U ON U.Id = B.OperatorChangeId ")
                .addField("U.UserId + '/' + U.UserName AS operatorChangeId")//制单人
                .addLeftJoin("TCW_DepositPlatform C ON C.Code = A.DepositPlatformCode ")
        ;
        return cond;
    }

    /**
     * 导出
     */

    @RequiresPermissions("tcwWanliSupplementary:export")
    @RequestMapping("/export")
    @ResponseBody
    public R export(@ModelAttribute TcwWanliSupplementaryItem cond)throws Exception {
        cond = setSeachSql(cond);
        cond.setOrderby("A.Id DESC");
        cond.setPage(0).setSize(999999);
        List<Map<String, Object>> listData = cond.queryMap();

        List<YsCol> listField = new YsCol()
                .setMenuId(MenuId.TCW_WanliSupplementary)
                .setFrontShow(YsColEnum.FrontShow.SHOW.value)
                .setOrderby("A.SortNo asc")
                .queryList();


        ExportExcelUtils<TcwWanliSupplementary> util = new ExportExcelUtils<TcwWanliSupplementary>(new TcwWanliSupplementary());

        String toFilePath = util.exportExcel(listData, listField, "万里汇补账单");
        UploadService.UploadDto dto = commonApiService.uploadLocalToServer(toFilePath);
        return R.ok(dto.getUrl());
    }




    /*
     * 新增
     */
    @RequestMapping("/add")
    @ResponseBody
    @RequiresPermissions("tcwWanliSupplementary:add")
    public R toAdd(HttpServletRequest request, @RequestParam Map<String, Object> params,String subData
    ) {
        try {
            logger.info("补账单新增传参：" + params);
            //修改
            return tcwWanliSupplementaryService.add(params,subData);
        } catch (Exception e) {
            logger.error("补账单新增出错：", e.getMessage(), e);
            return R.error("系统正在开小差，请联系客服，我们将第一时间为您解决！");
        }
    }

    /**
     * 修改
     */
    @RequestMapping("/toEdit")
    @ResponseBody
    public R toEdit(HttpServletRequest request, @RequestParam Map<String, Object> params,String subData
    ) {
        try {
            logger.info("补账单修改传参：" + params);
            //修改
            return tcwWanliSupplementaryService.edit(params,subData);
        } catch (Exception e) {
            logger.error("补账单修改出错：", e.getMessage(), e);
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return R.error("系统正在开小差，请联系客服，我们将第一时间为您解决！");
        }
    }

    /**
     * 流转通过
     */
    @RequiresPermissions("tcwWanliSupplementary:add")
    @PostMapping("/flowPass")
    @ResponseBody
    public R flowPass(Long id){

        TcwWanliSupplementary entity = new TcwWanliSupplementary().setId(id).queryFirst();
        try {
            return tcwWanliSupplementaryService.flowPass(entity.getBid(), ShiroUtils.getUserId());
        } catch (Exception e) {
            e.printStackTrace();
            return R.error("服务器正在开小差，请联系客服");
        }
    }

    /**
     * 流转驳回
     */
    @RequiresPermissions("tcwWanliSupplementary:add")
    @PostMapping("/flowRefuse")
    @ResponseBody
    public R flowRefuse(Long id, String reason){

        TcwWanliSupplementary entity = new TcwWanliSupplementary().setId(id).queryFirst();
        try {
            return tcwWanliSupplementaryService.flowRefuse(entity.getBid(), reason, ShiroUtils.getUserId());
        } catch (Exception e) {
            e.printStackTrace();
            return R.error("服务器正在开小差，请联系客服");
        }
    }


    /**
     * 流转驳回
     */
    @RequiresPermissions("tcwWanliSupplementary:add")
    @PostMapping("/flowInit")
    @ResponseBody
    public R flowInit(Long id, String reason){

        TcwWanliSupplementary entity = new TcwWanliSupplementary().setId(id).queryFirst();
        try {
            return yspTaskService.flowRefuse(entity.getBid(), reason, ShiroUtils.getUserId());
        } catch (Exception e) {
            e.printStackTrace();
            return R.error("服务器正在开小差，请联系客服");
        }
    }

    /**
     * 删除
     * @throws Exception
     */
    @PostMapping("/delete")
    @ResponseBody
    public R delete(Long id){

        try {
            return tcwWanliSupplementaryService.deleteTrue(id);
        } catch (Exception e) {
            e.printStackTrace();
            return R.error("操作失败，请联系客服");
        }
    }

}
        










